package com.kchouse;


import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kchouse.common.BaseFragment;

import static android.app.Activity.RESULT_OK;


/**
 * A simple {@link Fragment} subclass.
 */
public class SecCarFragment extends BaseFragment implements View.OnClickListener{


    private View view;
    private ImageView iv_photo1;
    private ImageView iv_photo2;
    private ImageView iv_photo3;
    private Intent intent=new Intent();
    private int IMAGE_REQUEST_PHOTO1CODE=101;//正面照请求码
    private int IMAGE_REQUEST_PHOTO2CODE=102;//侧面面照请求码
    private int IMAGE_REQUEST_PHOTO3CODE=103;//内饰照请求码
    private ListView lv_valuation;
    private ListView lv_valuation_people;
    private LayoutInflater inflater;

    public SecCarFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if(view==null)
        {
            this.inflater=inflater;
            view= inflater.inflate(R.layout.fragment_sec_car, null);
            intiData();
            intiView();
        }


        return view;
    }

    private void intiView() {

        iv_photo1 = (ImageView) view.findViewById(R.id.iv_photo1);
        iv_photo1.setOnClickListener(this);
        iv_photo2 = (ImageView) view.findViewById(R.id.iv_photo2);
        iv_photo2.setOnClickListener(this);
        iv_photo3 = (ImageView) view.findViewById(R.id.iv_photo3);
        iv_photo3.setOnClickListener(this);

        lv_valuation= (ListView)view.findViewById(R.id.lv_valuation);
        lv_valuation.setAdapter(new ValuationAdapter());
        lv_valuation_people= (ListView)view.findViewById(R.id.lv_valuation_people);
        lv_valuation_people.setAdapter(new ValuationPeopleAdapter());
    }

    private void intiData() {
    }

    class ValuationAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view= inflater.inflate(android.R.layout.simple_list_item_1,null);
            TextView text1 = (TextView) view.findViewById(android.R.id.text1);
            text1.setTextSize(13);
            text1.setText("询价师"+position+"：10"+position+"万");
            return view;
        }
    }

    class ValuationPeopleAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=inflater.inflate(android.R.layout.simple_list_item_1,null);
            TextView text1 = (TextView) view.findViewById(android.R.id.text1);
            text1.setText("询价师:XXX    手机号：1800121211（微信同号）");
            text1.setTextSize(13);
            return view;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_photo1:
                intent.setAction(Intent.ACTION_PICK);
                intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, IMAGE_REQUEST_PHOTO1CODE);
                break;
            case R.id.iv_photo2:
                intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(intent, IMAGE_REQUEST_PHOTO2CODE);
                break;
            case R.id.iv_photo3:
                intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(intent, IMAGE_REQUEST_PHOTO3CODE);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==IMAGE_REQUEST_PHOTO1CODE)
        {
            if (resultCode == RESULT_OK) {//resultcode是setResult里面设置的code值
                try {
                    Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String  path = cursor.getString(columnIndex);  //获取照片路径
                    cursor.close();
                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    iv_photo1.setImageBitmap(bitmap);
                } catch (Exception e) {
                    // TODO Auto-generatedcatch block
                    e.printStackTrace();
                }
            }//这里的requestCode是我自己设置的，就是确定返回到那个Activity的标志
        }
        if(requestCode==IMAGE_REQUEST_PHOTO2CODE)
        {
            if (resultCode == RESULT_OK) {//resultcode是setResult里面设置的code值
                try {
                    Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor =getActivity(). getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String  path = cursor.getString(columnIndex);  //获取照片路径
                    cursor.close();
                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    iv_photo2.setImageBitmap(bitmap);
                } catch (Exception e) {
                    // TODO Auto-generatedcatch block
                    e.printStackTrace();
                }
            }//这里的requestCode是我自己设置的，就是确定返回到那个Activity的标志
        }
        if(requestCode==IMAGE_REQUEST_PHOTO3CODE)
        {
            if (resultCode == RESULT_OK) {//resultcode是setResult里面设置的code值
                try {
                    Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String  path = cursor.getString(columnIndex);  //获取照片路径
                    cursor.close();
                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    iv_photo3.setImageBitmap(bitmap);
                } catch (Exception e) {
                    // TODO Auto-generatedcatch block
                    e.printStackTrace();
                }
            }//这里的requestCode是我自己设置的，就是确定返回到那个Activity的标志
        }

    }
}
